﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FlexminAPI;


namespace Remote_Desktop
{
	public partial class DesktopForm : Form
	{
		private AxMSTSCLib.AxMsRdpClient4 rdpc = null;

		private DeskSlave slave;
		private IHost host;

		public DesktopForm(ISlave slave, IHost host)
		{
			this.slave = slave as DeskSlave;
			this.host = host;

			if (!this.slave.AreServicesReady())
			{
				MessageBox.Show("RDP Service is not started", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				this.Close();
			}

			InitializeComponent();
		}

		protected override void OnCreateControl()
		{
			rdpc = new AxMSTSCLib.AxMsRdpClient4();
			rdpc.OnDisconnected += new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler(rdpc_OnDisconnected);
			this.Controls.Add(rdpc);
			rdpc.Dock = DockStyle.Fill;
//			rdpc.CreateControl();
			base.OnCreateControl();
			Connect();
		}

		public void Connect()
		{
			rdpc.Server = host.HostAddress;			
			rdpc.ColorDepth = Screen.PrimaryScreen.BitsPerPixel;
			rdpc.Location = new Point(0, 0);
			rdpc.Size = SystemInformation.PrimaryMonitorSize;
			rdpc.DesktopHeight = SystemInformation.PrimaryMonitorSize.Height;
			rdpc.DesktopWidth = SystemInformation.PrimaryMonitorSize.Width;
			rdpc.Connect();
		}

		private void rdpc_OnDisconnected(object sender, AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEvent e)
		{
			if (e.discReason != 0x0 || e.discReason != 0x1 || e.discReason != 0x2 || e.discReason != 0x3)
			{
				rdpc.Visible = false;
				toolStripButton1.Enabled = false;
				errorBox.Text = "Unable to connect.\r\nYou possibly use HOME version of windows";
				errorBox.Visible = true;
			}
			rdpc.Disconnect();
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			rdpc.FullScreen = true;
		}
	}
}
